/*
 * Copyright (c) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef __EVENT_LISTENER_H__
#define __EVENT_LISTENER_H__

#include <components/ui_view.h>
#include <functional>
#include <utility>

#include <ability_loader.h>

#include "securec.h"

namespace OHOS {
using OnEventFunc = std::function<bool(UIView &view, const Event &event)>;

class EventListener :
        public UIView::OnClickListener,
        public UIView::OnLongPressListener {
public:
    EventListener() = delete;
    ~EventListener() override = default;

    EventListener(OnEventFunc onClick, OnEventFunc onLongPress)
    {
        onClick_ = std::move(onClick);
        onLongPress_ = std::move(onLongPress);
    }

    bool OnClick(UIView& view, const ClickEvent &event) override
    {
        if (!onClick_) {
            return false;
        }
        UIView *currentView = &view;
        if (currentView == nullptr) {
            return false;
        }
        return onClick_(*currentView, event);
    }

    bool OnLongPress(UIView& view, const LongPressEvent &event) override
    {
        if (!onLongPress_) {
            return false;
        }
        UIView *currentView = &view;
        if (currentView == nullptr) {
            return false;
        }
        return onLongPress_(*currentView, event);
    }

private:
    OnEventFunc onClick_ {};
    OnEventFunc onLongPress_ {};
};

class HoodsButtonClickListener : public UIView::OnClickListener {
public:
    virtual ~HoodsButtonClickListener(){}
    HoodsButtonClickListener(AbilitySlice *slice) : slice_ (slice)
    {
        (void)memset_s(sliceName_, sizeof(sliceName_), 0x00, sizeof(sliceName_));
        (void)memset_s(data_, sizeof(data_), 0x00, sizeof(data_));
    }

    bool OnClick(UIView& view, const ClickEvent &event) override
    {
        if (slice_ == nullptr) {
            return false;
        }

        if (strlen(sliceName_) > 0) {
            AbilitySlice *nextSlice = nullptr;
            Want wantData = { nullptr };

            if (strlen(data_) > 0) {
                if (SetWantData(&wantData, data_, strlen(data_) + 1) < 0) {
                    printf("################### SetWantData failed! ############################ \n");
                    return false;
                }
            }

            nextSlice = AbilityLoader::GetInstance().GetAbilitySliceByName((char *)sliceName_);
            if (nextSlice == nullptr) {
                printf("################### undefined slice by name %s! ############################ \n", sliceName_);
                return false;
            }

            slice_->Present(*nextSlice, wantData);
        } else {
            slice_->TerminateAbility();
        }

        return true;
    }

    void SetSliceInfo(const char *sliceName, const char *data)
    {
        if (sliceName != nullptr) {
            (void)strcpy_s(sliceName_, sizeof(sliceName_), sliceName);
        }

        if (data != nullptr) {
            (void)strcpy_s(data_, sizeof(data_), data);
        }
    }

private:
    char sliceName_[256] = {0};
    char data_[256] = {0};
    AbilitySlice *slice_ { nullptr };
};

}
#endif // __EVENT_LISTENER_H__